package com.example.bookexchange;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class AddressActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private AddressAdapter addressAdapter;
    private List<Address> addressList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);

        // 初始化 RecyclerView
        recyclerView = findViewById(R.id.recyclerViewAddresses);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // 加载地址数据
        addressList = loadAddresses();

        // 初始化适配器并设置点击事件
        addressAdapter = new AddressAdapter(addressList, address -> {
            // 返回选中的地址给上一个界面（OrderDetailsActivity）
            Intent resultIntent = new Intent();
            resultIntent.putExtra("selectedAddress", address.getContactName() +" "+ address.getPhoneNumber() +"\n"+ address.getFullAddress()); // 将选中的地址传递给 OrderDetailsActivity
            setResult(RESULT_OK, resultIntent);  // 返回结果给启动的 Activity
            finish(); // 关闭当前 Activity
        });

        recyclerView.setAdapter(addressAdapter);
        // 添加地址按钮点击事件
        Button btnAddAddress = findViewById(R.id.btnAddAddress);
        btnAddAddress.setOnClickListener(v -> {
            Intent intent = new Intent(AddressActivity.this, AddAddressActivity.class);
            startActivity(intent);
        });
    }

    // 模拟加载地址数据，可以从数据库或API获取
    private List<Address> loadAddresses() {
        List<Address> addresses = new ArrayList<>();
        addresses.add(new Address("张三", "13800138000", "北京市 海淀区 清华大学"));
        addresses.add(new Address("李四", "13900139000", "上海市 浦东新区 陆家嘴"));
        addresses.add(new Address("王五", "13700137000", "广东省 广州市 天汇大厦"));
        // 添加更多的地址数据
        return addresses;
    }
}
